<?php
/**
 * class App_Form_Profile_View
 * profile form
 *
 */
class App_Form_Profile_View extends Zend_Form
{
    /**
     * init the form
     *
     * @return object
     */
    public function init()
    {
        $this->setMethod('post');

        // countries list
        $country = new App_Model_Country();
        $countries = $country->listAll();

        // foods list
        $food = new App_Model_Food();
        $foods = $food->listAll();

        // distances list
        $distance = new App_Model_Distance();
        $distances = $distance->listAll();

        // user id
        $this->addElement('hidden', 'user_id', array(
            'required'  => true,
        ));

        // firstname
        $this->addElement('text', 'firstname', array(
            'required'  => true,
            'label'     => 'First Name',
            'filters'   => array('StringTrim')
        ));

        // lastname
        $this->addElement('text', 'lastname', array(
            'required'      => true,
            'label'         => 'Last Name',
            'description'   => '(Last name initial will be shown in public.)',
            'filters'       => array('StringTrim')
        ));

        // gender
        $this->addElement('radio', 'gender', array(
            'required'  => false,
            'label'     => 'Gender',
            'separator' => ' ',
            'multiOptions' => array(
                '1' => 'Male',
                '2' => 'Female',
                '3' => 'Prefer not to say'
            )
        ));

        // birth_date
        $this->addElement('text', 'birth_date', array(
            'required'      => false,
            'label'         => 'Birthdate',
            'validators'    => array('Date'),
            'filters'       => array('StringTrim')
        ));

        // countries list
        $this->addElement('select', 'country_id', array(
            'required'  => false,
            'label'     => 'Your Country',
            'multiOptions' => $countries
        ));

        // states list
        $this->addElement('select', 'state_id', array(
            'required'  => false,
            'label'     => 'Your State',
            // should be set to false when using ajax
            'registerInArrayValidator' => false,
        ));

        // cities lists
        $this->addElement('select', 'city_id', array(
            'required'  => false,
            'label'     => 'Your City',
            'registerInArrayValidator' => false,
        ));

        // distances list
        $this->addElement('select', 'distance_id', array(
            'required'  => false,
            'label'     => "What's the maximum distance you regularly travel from home to eat?",
            'multiOptions' => $distances,
        ));

        // foods list
        $this->addElement('multiselect', 'user_food', array(
            'required'      => false,
            'label'         => 'Which types of foods do you like? (Hold down the CTRL key to select more than one)',
            'size'          => 8,
            'multiOptions'  => $foods
        ));

        // submit button
        $this->addElement('submit', 'submit', array(
            'ignore'    => true,
            'style'     => 'height:25px',
            'label'     => 'Update',
        ));

        // CSRF protection
        //$this->addElement('hash', 'csrf', array('ignore' => true));
    }

    /**
     * set user defaults data
     *
     * @param array $user
     * @return Zend_Form
     */
    public function setUserDefaults($data)
    {
        // set user id
        $this->user_id->setValue($data['id']);

        // set state list
        $state = new App_Model_State();
        $states = $state->getStatesByCountryId($data['country_id']);
        $this->state_id->setMultiOptions($states);

        /**
         * @todo set city list
         */
        $this->city_id->setMultiOptions(array($data['city_id'] => $data['city_name']));

        // set foods list
        $user = new App_Model_User();
        $foods = $user->getFoodIdsById($data['id']);
        $this->user_food->setValue($foods);

        // set default value for form elements
        $this->setDefaults($data);

        return $this;
    }

}